package com.genius.core.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.genius.model.Batch;
import com.genius.model.Student;
import com.genius.model.StudentBatch;
import com.genius.model.StudentLevel;
import com.genius.model.StudentPayment;
import com.genius.model.Teacher;

/**
 * Student DAO Impl
 * 
 * @author Manish Rathi [manish_rathi2001@yahoo.com]
 * @version 1.0
 */
public class StudentDAOImpl extends HibernateDaoSupport implements StudentDAO {
	
    /**
     * Create new student
     * @see com.genius.core.dao.StudentDAO#createStudent(com.genius.model.Student)
     */
	public void createStudent(Student student) {
	    
        if(student.getId() == null || student.getId().equals("")) {
            Date date = student.getDateOfAddmission();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            int year = c.get(Calendar.YEAR);
            int studentCount = getStudentCount(year);
            String strYear = (year + "").substring(2);
            student.setId(generateStudentId(studentCount + 1, strYear));
        }
        this.getHibernateTemplate().saveOrUpdate(student);
	}
    
    /**
     * Generate student Id
     * @param c
     * @param year
     * @return
     */
    private String generateStudentId(int c, String year) {
        String countStr = c + "";
        if (countStr.length() == 1) {
            countStr = "00" + countStr; 
        }
        if (countStr.length() == 2) {
            countStr = "0" + countStr; 
        }
        
        return "1904" + year + countStr;
    }
    
	public int getStudentCount(int year) {
        
        String strYear = (year + "").substring(2);
        System.out.println(strYear);
        Session session = this.getSession();
        Criteria c = session.createCriteria(Student.class);
        c.add(Restrictions.like("id","1904" + strYear +"%"));
        List sList = c.list();
        return sList.size();
    }
    
    
    public List getStudents (String firstName, String lastName) {
        Session session = this.getSession();
        Criteria c = session.createCriteria(Student.class);
        c.add(Restrictions.like("firstName",firstName +"%"));
        c.add(Restrictions.like("lastName",lastName +"%"));
        List sList = c.list();
        return sList;
    }
    
    public Student getStudent(String studentId) {
        return (Student)this.getHibernateTemplate().get(Student.class, studentId);
    }
    

	public List getStudentPaymentDetails(String studentId){
        
		Criteria c = this.getSession().createCriteria(StudentPayment.class);
		c.add(Restrictions.eq("studentId",studentId));
        List l = c.list();
        System.out.println("Payment List Size" + l.size());       
        return l;
        
        
		
	}

    /**
     * Generate Batch Id
     * @param c
     * @param year
     * @return
     */
    private String generateBatchId(int c, String year, int level) {
        String countStr = c + "";
        if (countStr.length() == 1) {
            countStr = "0" + countStr; 
        }
        
        String strLevel = "0";
        if((level+"").length()==1) {
            strLevel = strLevel + level;
        }

        return "1904" + year + strLevel + countStr;
    }

    
	public void createStudentPayment(StudentPayment studentPayment){
		//getStudentPaymentDetails("190401006");
		this.getHibernateTemplate().save(studentPayment);
	}
    
	public void createBatch(Batch batch){
        if(batch.getId() == null || batch.getId().equals("")) {
            Date date = batch.getStartDate();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            int year = c.get(Calendar.YEAR);
            int studentCount = getBatchCount(year, batch.getLevel());
            String strYear = (year + "").substring(2);
            
            batch.setId(generateBatchId(studentCount + 1, strYear, batch.getLevel()));
        }

        this.getHibernateTemplate().saveOrUpdate(batch);
        
	}
    
    public Batch getBatch(String batchId) {
        return (Batch)this.getHibernateTemplate().get(Batch.class, batchId);
    }
    /**
     * 
     * @see com.genius.core.dao.StudentDAO#getStudentsForBatch(java.lang.String)
     */
    public List getStudentsForBatch(String batchId) {
        
        Session session = this.getSession();
        Criteria c = session.createCriteria(StudentBatch.class);
        c.add(Restrictions.eq("batchId", batchId));
        List l = c.list();
        return l;
        
    }
    
    /**
     * 
     * @param batchId
     * @param studentId
     */
    public void addStudentToBatch(String batchId, String studentId) {
        
        StudentBatch sb = new StudentBatch();
        Batch b = new Batch();
        b.setId(batchId);
        Student s = new Student();
        s.setId(studentId);
        sb.setStudent(s);
        sb.setBatch(b);
        
        this.getHibernateTemplate().save(sb);
        
        
    }
    
    /**
     * 
     * @param batchId
     * @param studentIds
     */
    public void addStudentsToBatch(String batchId, List studentIds) {
    
        List list = new ArrayList();
        int size = studentIds.size();
        for(int i=0; i<size ; i++) { 
            StudentBatch sb = new StudentBatch();
            sb.setBatchId(batchId);
            Student s = new Student();
            s.setId((String)studentIds.get(i));
            sb.setStudent(s);
            list.add(sb);
        }
        this.getHibernateTemplate().saveOrUpdateAll(list);
        
        
    }

    /**
     * 
     * @param year
     * @param level
     * @return
     */
    private int getBatchCount(int year, int level) {
        
        String strYear = (year + "").substring(2);
        String strLevel = "0";
        if((level+"").length()==1) {
            strLevel = strLevel + level;
        }
        Session session = this.getSession();
        Criteria c = session.createCriteria(Batch.class);
        c.add(Restrictions.like("id","1904" + strYear + strLevel +"%"));
        List sList = c.list();
        return sList.size();
    }

    public void updateStudentLevel(StudentLevel studentLevel)
    {
        this.getHibernateTemplate().saveOrUpdate(studentLevel);
    }
    
    public void updateStudent(Student student)
    {
        this.getHibernateTemplate().update(student);
    }
    
    
    public void getStudentCount(String studentId, int levelId)
    {
        Student s = getStudent(studentId);
        s.setCurrentLevelId(levelId);
        this.getHibernateTemplate().update(s);
        //this.getHibernateTemplate()
    }

    public List getStudentLevelDetails(String studentId)
    {
        Session session = this.getSession();
        Criteria c = session.createCriteria(StudentLevel.class);
        c.add(Restrictions.eq("studentId", studentId));
        return c.list();
    }

    public List getActiveBatches()
    {
        Session session = this.getSession();
        Criteria c = session.createCriteria(Batch.class);
        c.add(Restrictions.eq("activeFlag", true));
        List l = c.list();
        return l;
        
    }
    
    public List getBatchList(int active)
    {
        Session session = this.getSession();
        Criteria c = session.createCriteria(Batch.class);
        if (active == 1) {
            c.add(Restrictions.eq("activeFlag", true));    
        } else if(active == 0) {
            c.add(Restrictions.eq("activeFlag", false));
        }
        List l = c.list();
        return l;
        
    }
    
    public List getAllBatches()
    {
        Session session = this.getSession();
        Criteria c = session.createCriteria(Batch.class);
        //c.add(Restrictions.eq("activeFlag", true));
        List l = c.list();
        return l;
        
    }


    public void createStudentLevel(StudentLevel level)
    {
        this.getHibernateTemplate().saveOrUpdate(level);
    }

    public void addTeacher(Teacher teacher)
    {
        this.getHibernateTemplate().saveOrUpdate(teacher);
        
    }

    public List getTeacherList()
    {
        Session session = this.getSession();
        Criteria c = session.createCriteria(Teacher.class);
        List l = c.list();
        return l;
    }

  
}
